<?php
//calculate user OT rate at 1.5, return the number for hours that is having 1.5 rate
	function calculateOTRate($date, $work_start_time, $end_time, $dayOfWeek) {
			
			//we first convert the timing to date time in php
			//mktime(hour, min, second, month, day, year
			$month = substr($date, 5, 2);
			$day = substr($date, 8, 2);
			$year = substr($date, 0, 4);
			
			
			
			$leaveTime = mktime(substr($end_time, 0, 2), substr($end_time, 2, 2),  0, $month, $day, $year);
			
			//official time that staff is suppose to leave
			//weekdays (normal): if work_start_time = 1000 then work_leave_time += 9hrs, 1900
			//weekdays (normal): if work_start_time = 1200 then work_leave_time += 9hrs, 2100
			//weekdays (halfday): if work_start_time = 1000 then work_leave_time += 4hrs, 1400
			$work_leave_time;
			if(date("l", $leaveTime) == "Saturday") {
				$work_leave_time = mktime((substr($work_start_time, 0, 2)) + 4, substr($work_start_time, 2, 2),  0, $month, $day, $year);
			}
			else {
				if($dayOfWeek == "NORMAL") {
					$work_leave_time = mktime((substr($work_start_time, 0, 2)) + 9, substr($work_start_time, 2, 2),  0, $month, $day, $year);
				}
				else { 
					$work_leave_time = mktime((substr($work_start_time, 0, 2)) + 4, substr($work_start_time, 2, 2),  0, $month, $day, $year);
				}
			}
			
			//for event when staff leave time is after midnight, we need to change the date to the next day, or else the system will assume
			//it is the midnight for that day
			if(date("H",$leaveTime) < 05) {
				
				$midnightTime = mktime(substr($end_time, 0, 2), substr($end_time, 2, 2),  0, $month, $day+1, $year);
				$OTmins = ($midnightTime - $work_leave_time)/60;
			}
			else {
				//we compare the 2 date time and find the difference between them, to found out how many staff work beyong 1900
				$OTmins = ($leaveTime - $work_leave_time)/60;
				
			}
			
			
			
			//echo "OT TIME : ".$OTmins;
			//echo "<BR>";
			if($OTmins <= 0) {
				return 0;
			}
			else {
				return calculateOTHours($OTmins);

			}
					
	}//end function
	
	function calculate2XRate($start_time, $end_time, $break_start, $break_end) {
		
		//covert start time to a date time format HH, MM, SS, MONTH, DAY, YEAR
		
		$start_time = mktime (substr($start_time, 0, 2), substr($start_time, 2, 2), 0, 01, 01, 2011);
		$end_time = mktime (substr($end_time, 0, 2), substr($end_time, 2, 2), 0, 01, 01, 2011);
		$OTmins = 0;
		
		$OTmins = date(($end_time - $start_time) / 60);
		//echo "WITHOUT BREAK : ".$OTmins;
		return calculateOTHours($OTmins);
		
	}
	
	function calculateOTHours($OTmins) {
				
		
		$OTHours = 0;
		$firstOThour = false;
	
		// >23mins round to 0.5 hours
		// >38mins round to 0.75 hours
		// >53mins round to 1hr hours
			
		if($OTmins >= 53) {
			$firstOThour = true;
			$OTHours += intval($OTmins/60);
			$OTmins -= ($OTHours * 60);
			if ($OTHours == 0) {
				$OTHours += 1;
				$OTmins = 0;
			}
		}
		
		if($OTmins >= 38) {
			$OTmins -= 45;
			$OTHours +=0.75;
		}	
		else if($OTmins >= 23) {
			$OTHours += 0.5;
			$OTmins -= 30;	
		}
		else if ($OTmins >= 8 && $firstOThour) {
			$OTHours += 0.25;
			$OTmins -= 15;
		}
		return $OTHours;
	}
	
	function calculateLateness($start_work_time, $start_time) {
		
		
		
		//poilcy time will be the by right timing that staff shoudl start work at
		$start_work_time = mktime(substr($start_work_time, 0, 2), substr($start_work_time, 2, 2), 0, 12, 31, 2011);
		$start_time = mktime(substr($start_time, 0, 2), substr($start_time, 2, 2), 0, 12, 31, 2011);
		
		$lateMins = ($start_time - $start_work_time) / 60;
		
		if($lateMins > 0)
			return $lateMins."mins";
		else
			return "";
	
	}
	
	function calculateBreakLateness($break_start, $break_end) {
		
		$break_start = mktime(substr($break_start, 0, 2), substr($break_start, 2, 2), 0, 12, 31, 2011);
		$break_end = mktime(substr($break_end, 0, 2), substr($break_end, 2, 2), 0, 12, 31, 2011);
		
		$lateMins = (($break_end - $break_start) /60) - 60;
		
		if ($lateMins > 0)
			return $lateMins."mins";
		else
			return "";
	}


?>